package com.clusterswisdom.horn.api.tmall.action.item;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.clusterswisdom.horn.api.ioc.HornAction;
import com.clusterswisdom.horn.api.ioc.HornResponse;
import com.clusterswisdom.horn.api.ioc.api.ApiConfig;
import com.clusterswisdom.horn.api.tmall.TmallInstance;
import com.clusterswisdom.horn.api.tmall.action.AbstractTmallAction;
import com.taobao.api.domain.Item;
import com.taobao.api.response.ItemsOnsaleGetResponse;

public class GetItemsOnsaleAction extends AbstractTmallAction implements HornAction{
	
	@Override
	public HornResponse execute(ApiConfig config, JSONObject param)
			throws Exception {
		List<Item> items = new ArrayList<Item>();
		int pageNo = 1;
		long totalNum = -1;
	
		do{
			param.put("page_no", pageNo++);
			ItemsOnsaleGetResponse response=(ItemsOnsaleGetResponse)TmallInstance.getInstance().executeRequest(param, "item", "itemsOnsaleGet");
			if(totalNum == -1){
				totalNum =response.getTotalResults();
			}
			if(response.getItems() != null){
				items.addAll(response.getItems());
			}
		}while(items.size() < totalNum);
		return new HornResponse(true, items);
	}
}
